﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace CarInfor.DAL
{
    public class InsuranceDetail
    {
        #region 属性定义

        /// <summary>
        /// 主键标识
        /// </summary>
        string id = "";
        public string ID
        {
            get { return id; }
            set { id = value; }
        }

        /// <summary>
        /// 保单ID
        /// </summary>
        string formID = "";
        public string FormID
        {
            get { return formID; }
            set { formID = value; }
        }
        /// <summary>
        /// 类型名称
        /// </summary>
        private string typeName = string.Empty;
        public string TypeName
        {
            get { return typeName; }
            set { typeName = value; }
        }

        /// <summary>
        /// 保额
        /// </summary>
        private decimal insuredAmount;
        public decimal InsuredAmount
        {
            get { return insuredAmount; }
            set { insuredAmount = value; }
        }


        /// <summary>
        /// 金额
        /// </summary>
        private decimal price;
        public decimal Price
        {
            get { return price; }
            set { price = value; }
        }
        #endregion

        #region 方法
        /// <summary>
        /// 无条件查询
        /// </summary>
        public DataTable Query()
        {
            DataSet ds = null;
            try
            {
                DB db = new DB();
                ds = db.GetSqlDataSet(this.GetSqlString(""));
            }
            catch (Exception e)
            {
                throw (e);
            }
            return ds.Tables[0];
        }


        /// <summary>
        /// 有条件查询
        /// </summary>
        public DataTable Query(string condition)
        {
            DataSet ds = null;
            try
            {
                DB db = new DB();
                ds = db.GetSqlDataSet(this.GetSqlString(condition));
            }
            catch (Exception e)
            {
                throw e;
            }
            return ds.Tables[0];
        }

        /// <summary>
        /// 根据主键查询
        /// </summary>
        public void QueryWithKey(string keyValue)
        {
            DataSet ds = null;
            try
            {
                string cond = "ID='" + keyValue + "'";
                DB db = new DB();
                ds = db.GetSqlDataSet(this.GetSqlString(cond));
                if (ds.Tables[0].Rows.Count > 0)
                {
                    id = ds.Tables[0].Rows[0]["编号"].ToString();
                    typeName = ds.Tables[0].Rows[0]["类型名称"].ToString();
                    formID = ds.Tables[0].Rows[0]["保单ID"].ToString();
                    insuredAmount = decimal.Parse(ds.Tables[0].Rows[0]["保额"].ToString());
                    price = decimal.Parse(ds.Tables[0].Rows[0]["金额"].ToString());
                }
                else
                {
                    id = "";
                    typeName = "";
                    formID = "";
                }
            }
            catch (Exception e)
            {
                throw (e);
            }
        }

        /// <summary>
        /// 获取查询Sql
        /// </summary>
        public string GetSqlString(string condition)
        {
            StringBuilder sql = new StringBuilder();
            try
            {
                sql.Append("SELECT ");
                sql.Append(" ID as 编号,FormID as 保单ID,TypeName as 险种名称,InsuredAmount as 保额,Price as 金额");
                sql.Append(" FROM InsuranceDetail");
                if (!string.IsNullOrEmpty(condition))
                {
                    sql.Append(" WHERE " + condition + " order by TypeName desc ");
                }
                else
                {
                    sql.Append(" order by TypeName desc");
                }
            }
            catch (Exception e)
            {
                throw (e);
            }
            return sql.ToString();

        }


        /// <summary>
        /// 通过主键更新
        /// </summary>
        public int UpdateWithKey(string keyValue)
        {
            StringBuilder sql = new StringBuilder();
            DB db = new DB();
            try
            {
                sql.Append("UPDATE InsuranceDetail SET ");
                sql.Append("[FormID]='" + format(formID) + "',");
                sql.Append("[TypeName]='" + format(typeName) + "',");
                sql.Append("[InsuredAmount]=" + insuredAmount + ",");
                sql.Append("[Price]=" + price);
                sql.Append(" WHERE ID='" + keyValue + "'");
            }
            catch (Exception e)
            {
                throw (e);
            }
            return db.ExecuteSql(sql.ToString());
        }


        /// <summary>
        ///添加一条记录
        /// </summary>
        public int Insert()
        {
            StringBuilder sql = new StringBuilder();
            DB db = new DB();
            try
            {
                sql.Append("INSERT into InsuranceDetail");
                sql.Append("([ID],[FormID],[TypeName],[InsuredAmount],[Price]) ");
                sql.Append(" VALUES(");
                sql.Append("'" + format(id) + "'");
                sql.Append(",'" + format(formID) + "'");
                sql.Append(",'" + format(typeName) + "'");
                sql.Append("," + insuredAmount);
                sql.Append("," + price);
                sql.Append(")");
            }
            catch (Exception e)
            {
                throw (e);
            }
            return db.ExecuteSql(sql.ToString());
        }

        /// <summary>
        /// 要求在调用IsDelete（）后使用
        /// 返回为true时，表示删除成功
        /// 返回为false时，表示删除失败
        /// </summary>
        public bool Delete(string keyValue)
        {
            StringBuilder sql = new StringBuilder();
            DB db = new DB();
            try
            {
                sql.Append(" DELETE FROM InsuranceDetail ");
                sql.Append(" WHERE ID='" + keyValue + "'");
                db.ExecuteSql(sql.ToString());
            }
            catch (Exception e)
            {
                throw (e);
            }
            return true;
        }

        /// <summary>
        ///根据条件删除
        /// </summary>
        public bool DeleteWithCondition(string condition)
        {
            StringBuilder sql = new StringBuilder();
            DB db = new DB();
            try
            {
                sql.Append(" DELETE FROM InsuranceDetail ");
                sql.Append(" WHERE " + condition);
                db.ExecuteSql(sql.ToString());
            }
            catch (Exception e)
            {
                throw (e);
            }
            return true;
        }
        /// <summary>
        /// 单引号的处理
        /// </summary>
        public string format(string myString)
        {
            return myString.Replace("'", "''");
        }

        #endregion
    }
}